package com.firebird.app.kotlin.stock.trend.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.ImageView
import android.widget.TextView
import com.firebird.app.kotlin.stock.trend.bean.News
import com.firebird.app.kotlin.stock.trend.utils.AsyncImageLoader
import com.firebird.app.kotlin.stock.trend.R
import java.text.DateFormat
import java.text.SimpleDateFormat
import java.util.*


/**
 * 新闻资讯要闻页面中的Listview空间的适配器
 * @author ljk
 * @date   2019年11月7日
 */
class NewsYwListViewAdapter(private val context: Context, private var newsList: MutableList<News>?) : BaseAdapter() {
    //private SimpleAsyncImageLoader asyncImageLoader;
    private var asyncImageLoader: AsyncImageLoader?=null
    private val df: DateFormat = SimpleDateFormat("HH:mm", Locale.CHINA)
    /**
     * 设置ListView中的数据
     * @param newsList
     */
    fun setDate(newsList: MutableList<News>?) {
        this.newsList = newsList
        notifyDataSetChanged()
    }

    override fun getCount(): Int {
        return if (newsList != null) {
            newsList!!.size
        } else 0
    }

    override fun getItem(position: Int): Any? {
        return if (newsList != null) {
            newsList!![position]
        } else null
    }

    override fun getItemId(position: Int): Long {
        return position.toLong()
    }

    fun clean() {
        newsList!!.clear()
    }

    override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
        var mConvertView = convertView
        var holder: ViewHolder? = null
        if (mConvertView == null) {
            holder = ViewHolder()
            mConvertView = LayoutInflater.from(context).inflate(R.layout.item_news_yw, parent, false)
            holder.img = mConvertView!!.findViewById<View>(R.id.img_item) as ImageView
            holder.tv_title = mConvertView!!.findViewById<View>(R.id.tv_title) as TextView
            holder.tv_time = mConvertView!!.findViewById<View>(R.id.tv_time) as TextView
            mConvertView!!.tag = holder
        } else {
            holder = mConvertView.tag as ViewHolder
        }
        val news = newsList!![position]
        val time = df.format(news.createTime)
        holder.tv_title!!.text = news.title
        holder.tv_time!!.text = time
        asyncImageLoader!!.DisplayImage(news.imgUrl!!, holder.img!!)
        /*String imgUrl = news.getImgUrl();
		holder.img.setTag(imgUrl);

		Drawable cacheDrawable = asyncImageLoader.loadDrawable(imgUrl, new ImageCallback() {
			@Override
			public void imageLoaded(String imgUrl, Drawable drawable) {
				ImageView imageView = (ImageView) listView.findViewWithTag(imgUrl);
				if(imageView != null){
					imageView.setImageDrawable(drawable);
				}
			}
		});

		if(cacheDrawable == null){
			holder.img.setBackgroundColor(Color.WHITE);
		}else {
			holder.img.setImageDrawable(cacheDrawable);
		}*/return mConvertView
    }

    /**
     * 用于保存ListView中重用的item视图的引用
     * @author ljk
     * @date   2019年10月28日
     */
    internal inner class ViewHolder {
        var img: ImageView? = null
        var tv_title: TextView? = null
        var tv_time: TextView? = null
    }

    init {
        //asyncImageLoader = new SimpleAsyncImageLoader();
        asyncImageLoader = AsyncImageLoader(context, 5)
    }
}